<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Actapply extends CI_Controller {
	var $tablefunc = 'actapply';
	var $fields = array('name','company','brand','position','email','weixin','phone','needs','other','addtime');
	var $funcarr = array('del');
	var $needlist=array('1'=>'O2O大数据营销应用','2'=>'产品创新开发大数据应用','3'=>'电子商务大数据营销应用','4'=>'企业大数据商业模式创新应用','5'=>'企业内部数据分析、数据挖掘需求','6'=>'IT或大数据人才需求','7'=>'其他参会需求');
		
	function __construct(){
		parent::__construct();
		$this->Lang_model->loadLang('admin');
		$this->load->helper('array');
		$this->load->model('Purview_model');
		$this->Data_model->setTable($this->tablefunc);  //
	}
	
	public function index(){
		$this->Purview_model->checkPurview($this->tablefunc);
		$post = $this->input->post(NULL,TRUE);
		$getwhere = array();
		$pagearr=array(
			'currentpage'=>	isset($post['currentpage'])&&$post['currentpage']>0?$post['currentpage']:1,
			'totalnum'=>$this->Data_model->getDataNum($getwhere),
			'pagenum'=>20
		);
		$data = $this->Data_model->getData($getwhere,'id desc',$pagearr['pagenum'],($pagearr['currentpage']-1)*$pagearr['pagenum']);
		$currentid=($pagearr['currentpage']-1)*$pagearr['pagenum']+1; //每页起始ID
		$funcstr=$this->Purview_model->getFunc($this->tablefunc,$this->funcarr);
		$funcstr.='<input type="button" class="btn" onclick="location.href=\''.site_aurl('actapply/toexcel').'\'" value="导出">';
		$res = array(
				'tpl'=>'list',
				'tablefunc'=>$this->tablefunc,
				'liststr'=>$this->_setlist($data,true,$currentid),
				'pagestr'=>show_page($pagearr),
				'funcstr'=>$funcstr,
		);
		$this->load->view($this->tablefunc,$res);
	}
	
	public function toexcel(){
		$this->Purview_model->checkPurview($this->tablefunc);
		$getwhere=array('activityname'=>1);
		$info = $this->Data_model->getData($getwhere,'id desc');
		$data=array();
		$data[0]=array('序号','公司名称','品牌名称','姓名','职位','电话','邮箱','微信','需求','报名时间');
		$autoid=1;
		foreach($info as $item){
			if(!empty($item['needs'])){
				$needs=explode(',',$item['needs']);
				$needsstr='';
				foreach($needs as $val){
					if($val!='7'){	
						$needsstr.=$this->needlist[$val].'; ';
					}
				}
				if(in_array('7',$needs)){
					$needsstr.='其他：'.$item['other'];
				}
				$item['needs']=	$needsstr;
			}
			$data[]=array($autoid,$item['company'],$item['brand'],$item['name'],$item['position'],$item['phone'],$item['email'],$item['weixin'],$item['needs'],date('Y-m-d H:i:s',$item['addtime']));
			$autoid++;
		}
		
		//print_r($data);
		$filename=date('Ymd').'_'.time();
		$this->load->library('excel');
		$xls = new Excel('UTF-8', true, 'Sheet 1');	
		$xls->addArray($data);
		$xls->generateXML($filename);	
	}

	public function view(){
		
		$this->Purview_model->checkPurviewAjax($this->tablefunc,'edit');
		$post = $this->input->post(NULL,TRUE);
		if($post['id']&&$post['action']==site_aurl($this->tablefunc)){
			if($this->Data_model->getSingle(array('mname'=>$post['mname'],'id !='=>$post['id']))){
				show_jsonmsg(array('status'=>206));
			}
			$time = time();
			$data = elements($this->fields,$post);
			$data['lasttime'] = $time;
			if($post['password']!=''){
				$this->load->helper('string');
				$salt = random_string('alnum',6);
				$data['mpwd'] = md5pass($post['password'],$salt);
				$data['mrnd'] = $salt;
			}
			$datawhere = array('id'=>$post['id']);
			$this->Data_model->editData($datawhere,$data);
			show_jsonmsg(array('status'=>200,'id'=>$post['id'],'remsg'=>$this->_setlist($this->Data_model->getSingle(array('id'=>$post['id'])),false)));
		}else{
			$id = $this->uri->segment(4);
			if($id>0 && $view = $this->Data_model->getSingle(array('id'=>$id))){
				if(!empty($view['needs'])){
					$needs=explode(',',$view['needs']);
					$needsstr='';
					foreach($needs as $val){
						if($val!='7'){	
							$needsstr.=$this->needlist[$val].'<br>';
						}
					}
					if(in_array('7',$needs)){
						$needsstr.='其他：'.$view['other'];
					}
					$view['needs']=	$needsstr;
				}
				
				$res = array(
						'tpl'=>'view',
						'tablefunc'=>$this->tablefunc,
						'view'=>$view,
				);
				show_jsonmsg(array('status'=>200,'remsg'=>$this->load->view($this->tablefunc,$res,true))); //
			}else{
				show_jsonmsg(array('status'=>203));
			}
		}
	}
	
	public function del(){
		$this->Purview_model->checkPurviewAjax($this->tablefunc,'del');
		$ids = $this->input->post('optid',true);
		if($ids){
			if(	$this->Data_model->delMemberData($ids)){
				show_jsonmsg(array('status'=>200,'remsg'=>lang('delok'),'ids'=>$ids));
			}else{
				how_jsonmsg(array('status'=>207));
			}	
			
		}else{
			show_jsonmsg(array('status'=>203));
		}
	}

	public function check(){
		$this->_setState('check','ischeck',1,'checkok');
	}
	public function uncheck(){
		$this->_setState('check','ischeck',0,'uncheckok');
	}
	public function disable(){
		$this->_setState('disable','isdisable',1,'disableok');
	}
	public function enable(){
		$this->_setState('disable','isdisable',0,'enableok');
	}


	function _setlist($data,$ismultiple=true,$currentid=1){
		$newdata = $ismultiple?$data:($newdata[0]=$data);
		if($ismultiple){
			$newdata = $data;
		}else{
			$newdata = array(0=>$data);
		}
		$newstr = '';
		$y=$currentid;
		foreach($newdata as $key=>$item){
			$item['func'] = '';
			if($this->Purview_model->checkPurviewFunc($this->tablefunc,'view')){
				$item['func'] .= $this->Purview_model->getSingleFunc(site_aurl($this->tablefunc.'/view/'.$item['id']),'view');
			}
			if($this->Purview_model->checkPurviewFunc($this->tablefunc,'del')&&$item['id']>1){
				$item['func'] .=  $this->Purview_model->getSingleFunc(site_aurl($this->tablefunc.'/del/'.$item['id']),'sdel',$item['id']);	
			}
			
			$newstr .='<tr id="tid_'.$item['id'].'">
			<td width=4% ><input type=checkbox name="optid[]"  value='.$item['id'].'></td>
			<td width=5%>'.$y.'</td>
			<td width=15%>'.$item['company'].'</td>
			<td width=11%>'.$item['brand'].'</td>
			<td width=9%>'.$item['name'].'</td>
			<td width=10%>'.$item['position'].'</td>
			<td width=10%>'.$item['phone'].'</td>
			<td width=10%>'.$item['email'].'</td>
			<td width=10%>'.$item['weixin'].'</td>
			<td width=10%>'.date('Y-m-d',$item['addtime']).'</td>
			<td width=6% >'.$item['func'].'</td></tr>';
			$y++;
		}
		return $newstr;
	}
}